package co.fitstart.admin.web.controller.finance;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.web.utils.WebUtils;

import co.fitstart.admin.entity.security.Login;
import co.fitstart.admin.service.PrincipalService;
import co.fitstart.admin.service.ReceivablesService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.admin.web.resolver.Secure;
import co.fitstart.admin.web.view.ExcelTemplateView;
import co.fitstart.entity.finance.Receivables;
import co.fitstart.entity.principal.Principal;

@Controller
@RequestMapping("/receivables")
@MenuSetting("menu-finance")
public class ReceivablesController {
    
    @Autowired
    private ReceivablesService receivablesService;
    
    @Autowired
    private PrincipalService principalService;
    
    /**
     * 
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-receivables-management")
    public ModelAndView list(@RequestParam(name = "export", required = false, defaultValue = "false") boolean export,
                             Page page,
                             HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("finance/receivables/receivables-list");
        
        List<Receivables> receivables = receivablesService.list(export, page);
        modelAndView.addObject("receivables", receivables);
        
        modelAndView.addObject("page", page);
        modelAndView.addObject("export", export);
        
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/export-view", method = RequestMethod.GET)
    public ModelAndView export() {
        ModelAndView modelAndView = new ModelAndView("finance/receivables/receivables-export");
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param login
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView onExport(@Secure Login login) {
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("receivables-record.xls"));
        
        // list all unexport student
        List<Receivables> receivables = receivablesService.list(false);
        modelAndView.addObject("receivables", receivables);
        
        Map<Long, Principal> managers = principalService.listSiteManager();
        modelAndView.addObject("managers", managers);
        
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        modelAndView.addObject("fmt", format);
        
        
        // update receivables's export
        if(!receivables.isEmpty()) {
            List<Long> receivablesIds = CollectionUtils.extractPropertyList(receivables, "id");
            receivablesService.export(receivablesIds, login.getId());
        }
        
        return modelAndView;
    }

}
